package com.atgugu.flink.chapter11.tvf;

import com.atgugu.flink.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @Author lzc
 * @Date 2022/4/12 8:59
 */
public class Flink04_TVF_GroupingSets {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
        SingleOutputStreamOperator<WaterSensor> stream = env
            .fromElements(
                new WaterSensor("sensor_1", 1001L, 1),
                new WaterSensor("sensor_1", 3000L, 3),
                new WaterSensor("sensor_1", 4000L, 4),
                new WaterSensor("sensor_2", 2000L, 2),
                new WaterSensor("sensor_2", 5000L, 5),
                new WaterSensor("sensor_1", 6001L, 6)
            )
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                    .withTimestampAssigner((ws, ts) -> ws.getTs())
            );
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        Table table = tEnv.fromDataStream(stream, $("id"), $("ts").rowtime(), $("vc"));
        tEnv.createTemporaryView("sensor", table);
        
        // 分组集
        /*tEnv
            .sqlQuery("select" +
                          " ' ' id, " +
                          " window_start, " +
                          " window_end, " +
                          " sum(vc) vc_sum " +
                          "from table( tumble(table sensor, descriptor(ts), interval '5' second) )" +
                          "group by window_start, window_end " +
                          "union " +
                          "select" +
                          " id, " +
                          " window_start, " +
                          " window_end, " +
                          " sum(vc) vc_sum " +
                          "from table( tumble(table sensor, descriptor(ts), interval '5' second) )" +
                          "group by id, window_start, window_end ")
            .execute()
            .print();*/
    
        tEnv
            .sqlQuery("select" +
                          " id, " +
                          " window_start, " +
                          " window_end, " +
                          " sum(vc) vc_sum " +
                          "from table( tumble(table sensor, descriptor(ts), interval '5' second) )" +
                          "group by window_start, window_end, grouping sets( (id), () ) "
                       )
            .execute()
            .print();
            
        
        
    }
}
/*



 */